package mysql

import (
	"fmt"
	"goods_srv/appconfig"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	Db  *gorm.DB
	err error
)

func Init() {

	appConfig := appconfig.AppConfig.MySQLConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True", appConfig.User, appConfig.Password, appConfig.Host, appConfig.Port, appConfig.Database)
	Db, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("数据库连接失败")
	} else {
		fmt.Println("数据库连接成功")
	}
	err = Db.AutoMigrate(&Goods{})
	if err != nil {
		panic(err)
	}
}
